<?php
/**
 * Created by jinqiang on 2024/6/12 10:00.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Banshi;


use App\Http\Controllers\Controller;
use App\Http\Services\Banshi\DesignService;
use Illuminate\Http\Request;

class DesignController extends Controller
{
    public function list(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required|integer|max:20'
        ]);
        $param = $request->all();
        $res = $designService->search($param);
        return result($res);
    }

    public function detail(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'number' => 'required'
        ]);
        $param = $request->all();
        $number = $param['number'];
        $res = $designService->detail($number);
        return result($res);
    }

    //缩水率
    public function detailMaterialSr(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required',
            'style_bom_id' => 'required',
            'style_id' => 'required',
            'sr' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->detailMaterialSave($param);
        return $res['code'] === 0 ? success("修改成功") : error($res['msg'], $res);
    }

    //尺寸表信息
    public function detailRule(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required'
        ]);
        $param = $request->all();
        $typeNo = $param['type_no'];
        $res = $designService->detailRule($typeNo);
        return result($res);
    }

    //尺寸表保存
    public function detailRuleSave(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required',
            'data' => 'required'
        ]);
        $param = $request->all();
        $id = (Int)$param['id'];
        $paramData = json_decode($param['data'], true);
        $res = $designService->detailRuleSave($id, $paramData);
        return $res['code'] === 0 ? success("保存成功") : error($res['msg'], $res);
    }

    //解锁、锁定
    public function detailLock(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required',
            'type_no' => 'required',
            'type' => 'required',
            'status' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->setLock($param);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //工艺单
    public function detailGy(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required',
            'is_edit' => 'required|in:0,1'
        ]);
        $param = $request->all();
        $res = $designService->detailGy($param['type_no'], (Int)$param['is_edit']);
        return result($res);
    }

    //工艺单--保存
    public function detailGySave(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'data' => 'required'
        ]);
        $param = $request->all();
        $data = json_decode($param['data'], true);
        $res = $designService->detailGySave($data);
        return $res['code'] === 0 ? success("保存成功") : error($res['msg'], $res);
    }

    //绣花bom--保存
    public function detailXhSave(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'style_id' => 'required',
            'bom_xh_data' => 'required',
        ]);
        $param = $request->all();
        $res = $designService->saveBomXH(json_decode($param['bom_xh_data'], true), $param['style_id']);
        return $res['code'] === 0 ? success("操作成功", $res) : error($res['msg'], $res);
    }

    //附件列表
    public function detailAttach(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required'
        ]);
        $param = $request->all();
        $data = $designService->attachList($param['type_no']);
        return result($data);
    }

    //附件删除
    public function detailAttachDel(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->attachDel((Int)$param['id']);
        return $res['code'] === 0 ? success("删除成功") : error($res['msg'], $res);
    }

    //相关资料
    public function detailFile(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->detailFileList($param);
        return result($res);
    }

    //相关资料--保存
    public function detailFileSave(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required',
            'type_no' => 'required',
            'group_id' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->detailFileSave($param);
        return $res['code'] === 0 ? success("保存成功") : error("保存失败", $res);
    }

    //停用、启用
    public function stop(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required',
            'status' => 'required|in:0,4'
        ]);
        $param = $request->all();
        $res = $designService->stop((Int)$param['id'], (Int)$param['status']);
        return $res['code'] === 0 ? success("操作成功") : error("操作失败", $res);
    }

    //设计档案--流程确认
    public function check(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'id' => 'required',
            'state' => 'required|in:0,1,2,3'
        ]);
        $param = $request->all();
        $res = $designService->check((Int)$param['id'], (Int)$param['state']);
        return $res['code'] === 0 ? success("操作成功") : error("操作失败", $res);
    }

    //设计档案--款式图片保存
    public function imageSave(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required',
            'data' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->imageSave($param['type_no'], json_decode($param['data'], true));
        return $res['code'] === 0 ? success("操作成功", $res) : error("操作失败", $res);
    }

    //设计档案--查重
    public function dbCheck(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required',
        ]);
        $param = $request->all();
        $res = $designService->dbCheck($param['type_no']);
        return $res['code'] === 0 ? success("操作成功", $res) : error($res['msg']);
    }

    //设计档案--删除
    public function del(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'type_no' => 'required',
        ]);
        $param = $request->all();
        $res = $designService->del($param['type_no']);
        return $res['code'] === 0 ? success("操作成功", $res) : error($res['msg']);
    }

    //设计档案--保存
    public function save(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'data' => 'required',
            'materiel_data' => 'required',
            'style_colour' => 'required',
            'bom_data' => 'required',
            'yy_data' => 'required',
            'images' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->save($param);
        return $res['code'] === 0 ? success("操作成功", $res) : error($res['msg'], $res);
    }

    //设计档案--物料在用款号
    public function materialStyle(Request $request, DesignService $designService)
    {
        $this->validate($request, [
            'is_product' => 'required',
            'materiel_number' => 'required'
        ]);
        $param = $request->all();
        $res = $designService->getMaterielStyle($param['materiel_number'], $param['is_product']);
        return result($res);
    }
}
